from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.error import HTTPError

def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        print(e) 
    try:
        bsObj = BeautifulSoup(html)
        title = bsObj.h1
    except AttributeError as e:
        print(e)
    return title
title = getTitle("http://www.baidu.com")
if title == None:
    print("title couldn't be found")
else:
    print(title)